package com.vincent.template.controller.manage;

import com.vincent.template.commons.base.VctVo;
import com.vincent.template.dto.sys.auth.AuthDto;
import com.vincent.template.service.logic.AuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Vincent Wang
 * @date 2021/2/28 20:27
 */
@Api(tags = "权限管理")
@RestController
@RequestMapping("/manage/auth")
public class AuthController {

    @Autowired
    private AuthService authService;

    @ApiOperation("查询所有权限")
    @GetMapping
    public List<AuthDto> findAuths() {
        return authService.findAuths();
    }

    @ApiOperation("根据id查询权限")
    @GetMapping("/{id}")
    public AuthDto findAuthById(@PathVariable("id") Long id) {
        return authService.findAuthById(id);
    }

    @ApiOperation("添加权限")
    @PostMapping("/create")
    public VctVo<Long> addAuth(@Validated @RequestBody AuthDto authDto) {
        return VctVo.id(authService.addAuth(authDto));
    }

    @ApiOperation("更新权限")
    @PutMapping("/{id}/update")
    public boolean updateAuth(@PathVariable("id") Long id,
                              @Validated @RequestBody AuthDto authDto) {
        return authService.updateAuth(id, authDto);
    }

    @ApiOperation("删除权限")
    @DeleteMapping("/{id}/delete")
    public boolean deleteAuth(@PathVariable("id") Long id) {
        return authService.deleteAuth(id);
    }
}
